// pages/suggest/detail.js
import {
    station,
    user
} from '../../api/apiData'
var pageNum1 = 1; //页码
Page({

    /**
     * 页面的初始数据
     */
    data: {
        info: '',
        active: 0,
        active: 1,
        steps: [{
                text: '事件提交',
            },
            {
                text: '审核通过',
            },
            {
                text: '处理中',
            },
            {
                text: '结果反馈',
            },
        ],
        steps2: [{
                text: '事件提交',
            },
            {
                text: '审核未通过',
            },
            {
                text: '处理中',
            },
            {
                text: '结果反馈',
            },
        ],
        content: '',
        feedback_id: '',
        loadMoreText: '加载中...', //底部文字
        list: [],
        total: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            feedback_id: options.id
        })
        station.feedback.detail({
            feedback_id: options.id
        }, res => {
            let status = res.data.status
            this.setData({
                info: res.data,
                active: status == 1 ? '0' : status == 3 ? '2' : status == 4 ? '3' : ''
            })
        })
        this.getList(1)

    },
      // 图片预览
      toPre(e){
        let that = this
        let images = e.currentTarget.dataset.item
        let current = e.currentTarget.dataset.src
        let arr = []
        images.forEach(item=>{
            arr.push(item.url)
        })
        wx.previewImage({
            current: current, // 当前显示图片的http链接
            urls: arr // 需要预览的图片http链接列表
          })
    },
    // 获取列表
    getList(page) {
        var that = this
        user.comment.lists({
            page: page,
            size: 10,
            module: 'station_feedback',
            module_id: that.data.feedback_id
        }, res => {
            that.setData({
                total: res.data.total
            })
            if (res.data.list.length > 0) {
                that.setData({
                    list: that.data.list.concat(res.data.list) //合并数据
                })
                if (11 > res.data.list.length > 0) {
                    that.setData({
                        loadMoreText: '' //没有更多了
                    })
                }

            } else {
                if (page !== 1) {
                    that.setData({
                        loadMoreText: '暂无数据'
                    })
                } else {
                    that.setData({
                        list: [],
                        loadMoreText: '暂无数据'
                    })
                }
            }
            wx.stopPullDownRefresh();
        })
    },
    // 点赞
    toLike(e) {
        let id = e.currentTarget.dataset.id
        let index = e.currentTarget.dataset.index
        user.like.submit({
            module: 'user_comment',
            module_id: id
        }, res => {
            if (res.code == 1) {
                wx.showToast({
                    title: res.msg,
                })
                let list = this.data.list
                if (list[index].has_like == 0) {
                    list[index].has_like = 1
                    list[index].like_num = list[index].like_num + 1
                } else {
                    list[index].has_like = 0
                    list[index].like_num = list[index].like_num - 1
                }
                this.setData({
                    list: list,
                })
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    bindinput(e) {
        var val = e.detail.value
        this.setData({
            content: val
        })
    },
    toSend() {
      let obj = {
        module:'station_feedback',
        module_id:this.data.feedback_id,
        content:this.data.content
      }
      let that = this
      user.comment.submit(obj,res=>{
          if(res.code==1){
              wx.showToast({
                title: res.msg,
              })
              that.setData({
                  content:''
              })
              that.onPullDownRefresh()
          }else{
              wx.showToast({
                title: res.msg,
                icon:'error'
              })
          }
      })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        pageNum1 = 1
        this.setData({
            list: [],
        });
        this.getList(1)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        pageNum1 += 1;
        this.getList(pageNum1)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})